<?php
	if ($mod=="group"){
		if ($act == 'main_add') {
			$res = $realstateclass->product_main_group_add($name);
			if ($res == 0) $err->AddError(17);
			$smarty->assign("goto", "tpl_group_filter=".$res."&tpl_realstate_module=groups");
		} elseif ($act == 'add') {
			$res = $realstateclass->product_group_add($name, $parent, $type);
			if ($res > 0) {
				$mysqlclass->mysql_my_query("INSERT INTO `".$setupdata['table_prefix']."users_depend` (`id`,`page_id`,`users_id`,`module`) VALUES ('', ".$res.", ".$_SESSION['login_user'].", 'realstate_module_group')", __LINE__, __FILE__);
			}
			if ($res == 0) $err->AddError(17);
			if (isset($_POST['tpl_group_filter']) && (intval($_POST['tpl_group_filter']) > 0)) $tpl_group_filter = $_POST['tpl_group_filter'];
			else $tpl_group_filter = 0;
			$smarty->assign("goto", "tpl_realstate_module=goods&group_id=".$res."&tpl_group_filter=".$tpl_group_filter);
		} elseif ($act == 'del_mas') {
			$del_mas = array();
			if (isset($_GET["ids"])) $del_mas=$_GET["ids"];
			elseif (isset($_POST["ids"])) $del_mas=$_POST["ids"];
			if (count($del_mas) > 0){
				$realstateclass->product_group_del($del_mas);
			}
		} elseif ($act == 'mod') {
			if (isset($_POST["name"]) && isset($_POST["group_id"]))
				$realstateclass->product_group_mod($_POST["group_id"], $_POST["name"]);
		}
	} elseif ($mod=="product"){
		if ($act == 'copy') {
			$del_mas = array();
			if (isset($_GET["ids"])) $del_mas=$_GET["ids"];
			elseif (isset($_POST["ids"])) $del_mas=$_POST["ids"];
			if (count($del_mas) > 0){
				$realstateclass->product_copy_to_group($parent, $del_mas);
			}
		}elseif ($act == 'move') {
			$del_mas = array();
			if (isset($_GET["ids"])) $del_mas=$_GET["ids"];
			elseif (isset($_POST["ids"])) $del_mas=$_POST["ids"];
			if (count($del_mas) > 0){
				$realstateclass->product_move_to_group($parent, $del_mas);
			}
		}elseif ($act == 'rename') {
			if ($parent != NULL){
				if (strlen($name)  > 0) $realstateclass->product_rename($parent, $name);
				else $err->AddError(5);
			}
		}elseif ($act == 'hide') {
			if ($hide != NULL){
				$product_id = NULL;
				if (!isset($_GET['product_id'])) $product_id=NULL; else $product_id=$_GET['product_id'];
				if ($product_id==NULL) if (!isset($_POST['product_id'])) $product_id=NULL; else $product_id=$_POST['product_id'];
				$realstateclass->product_hide($product_id, $hide);
			}
		}elseif ($act == 'up') {
			$group_id = NULL; $product_id = NULL;
			if (!isset($_GET['group_id'])) $group_id=NULL; else $group_id=$_GET['group_id'];
			if ($group_id==NULL) if (!isset($_POST['group_id'])) $group_id=NULL; else $group_id=$_POST['group_id'];
			if (!isset($_GET['product_id'])) $product_id=NULL; else $product_id=$_GET['product_id'];
			if ($product_id==NULL) if (!isset($_POST['product_id'])) $product_id=NULL; else $product_id=$_POST['product_id'];
			if (($group_id!=NULL) && ($product_id!=NULL)) $realstateclass->product_pos($group_id, $product_id, "up");				
		} elseif ($act == 'down'){
			$group_id = NULL; $product_id = NULL;
			if (!isset($_GET['group_id'])) $group_id=NULL; else $group_id=$_GET['group_id'];
			if ($group_id==NULL) if (!isset($_POST['group_id'])) $group_id=NULL; else $group_id=$_POST['group_id'];
			if (!isset($_GET['product_id'])) $product_id=NULL; else $product_id=$_GET['product_id'];
			if ($product_id==NULL) if (!isset($_POST['product_id'])) $product_id=NULL; else $product_id=$_POST['product_id'];
			if (($group_id!=NULL) && ($product_id!=NULL)) $realstateclass->product_pos($group_id, $product_id, "down");				
		} elseif ($act == 'add') {
			$res = $realstateclass->product_add($name, $parent, $lang);
			if ($res == 0) $err->AddError(15);
			if (isset($_POST['tpl_group_filter']) && (intval($_POST['tpl_group_filter']) > 0)) $tpl_group_filter = $_POST['tpl_group_filter'];
			else $tpl_group_filter = 0;
			$smarty->assign("goto", "tpl_realstate_module=product&product_id=".$res."&group_id=".$parent."&tpl_group_filter=".$tpl_group_filter);
		} elseif ($act == 'del_mas') {
			$del_mas = array();
			if (isset($_GET["ids"])) $del_mas=$_GET["ids"];
			elseif (isset($_POST["ids"])) $del_mas=$_POST["ids"];
			if (count($del_mas) > 0){
				$realstateclass->product_del($del_mas);
			}
		} elseif ($act == 'mod'){
			$fields = $realstateclass->get_product_fields($parent);
			$product_lang = $lang;
			if (isset($_GET["product_lang"])) $product_lang=$_GET["product_lang"];
			elseif (isset($_POST["product_lang"])) $product_lang=$_POST["product_lang"];
			$product_fields = $fields[$product_lang];
			foreach($product_fields as $value){
				$n = $value['name'];
				$t = $value['type_of_value'];
				if ($t == 'price'){
					if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
					if ($v == "") $v = 0;
					else $v = floatval($v);
				} else if ($t == 'super_select'){
					if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
					if (($v != "") && is_array($v)){
						$v = $realstateclass->mod_super_select($parent, $n, $v);
					} else $v = "";
				} else if ($t == 'float'){
					if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
					if ($v == "") $v = 0;
					else $v = (float) $v;
				} else if ($t == 'number'){
					if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
					if ($v == "") $v = 0;
					else $v = (int) $v;
				} else if ($t == 'date'){
					if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
					if ($v == "") {
						$v = "";
					} else {
						$tmp = explode(".",  $v);
						$v = mktime($tmp[3], $tmp[4], $tmp[5], $tmp[1], $tmp[2], $tmp[0]);
						if ($v == -1) {
							$v = "";
						}
					}
				} else if ($t == 'file'){
					if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
					if ($v == "0"){
						$file = $_FILES[$n];
						$file_name1 = $_FILES[$n]['name'];
						$v = $file['tmp_name'];
						if ($v != ""){
							if (isset($setupdata['file_size']) && ($file['size']/1024 > $setupdata['file_size'])) $err->AddError(28);
							else {
								$today = uniqid("");
								$size = $file['size'];
								$name = $parent."-".$today."-".$size."-".$file['name'];
								$file_to = $name;
								if (strlen($file_name1) <= 0) $file_name1=$file['name'];
								if (!(file_exists($setupdata['binary_path']."/".$parent))){
									if ((mkdir($setupdata['binary_path']."/".$parent, 0777)) && (copy($file['tmp_name'], $setupdata['binary_path']."/".$parent."/".$file_to))){
										if (file_exists( $setupdata['binary_path']."/".$parent."/".$file_to)){
											chmod($setupdata['binary_path']."/".$parent."/".$file_to, 0666);
											$v = $fileclass->file_add($file, $file_name1, $file_to, $parent, $_SESSION['login_user']);
										}
									}
								} else {
									if (copy($file['tmp_name'], $setupdata['binary_path']."/".$parent."/".$file_to)){
										if (file_exists($setupdata['binary_path']."/".$parent."/".$file_to)){
											chmod($setupdata['binary_path']."/".$parent."/".$file_to, 0666);
											$v = $fileclass->file_add($file, $file_name1, $file_to, $parent, $_SESSION['login_user']);
										}
									}
								}
							}
						}
					}						
				} else if ($t == 'flash'){
					if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
					if ($v == "0"){
						$file = $_FILES[$n];
						$file_name1 =$_FILES[$n]['name'];
						$v = $file['tmp_name'];
						if ($v != ""){
							if (isset($setupdata['file_size']) && ($file['size']/1024 > $setupdata['file_size'])) $err->AddError(28);
							else {
								$imagedata = getimagesize($v);
								$today = uniqid("");
								$name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".swf";
								$file_to = $name;
								if (strlen($file_name1) <= 0) $file_name1=$file['name'];
								if (!(file_exists($setupdata['binary_path']."/".$parent))){
									if ((mkdir($setupdata['binary_path']."/".$parent, 0777)) && (copy($file['tmp_name'], $setupdata['binary_path']."/".$parent."/".$file_to))){
										if (file_exists($setupdata['binary_path']."/".$parent."/".$file_to)){
											chmod($setupdata['binary_path']."/".$parent."/".$file_to, 0666);
											$v = $imageclass->flash_add($file, $file_name1, $file_to, $parent, $imagedata[0], $imagedata[1], $_SESSION['login_user']);
										}
									}
								} else {
									if (copy($file['tmp_name'], $setupdata['binary_path']."/".$parent."/".$file_to)){
										if (file_exists($setupdata['binary_path']."/".$parent."/".$file_to)){
											chmod($setupdata['binary_path']."/".$parent."/".$file_to, 0666);
											$v = $imageclass->flash_add($file, $file_name1, $file_to, $parent, $imagedata[0], $imagedata[1], $_SESSION['login_user']);
										}
									}
								}
							}
						}
					}
				} else if ($t == 'image'){
					if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
					if ($v == "0"){
						$file = $_FILES[$n];
						$file_name1=$_FILES[$n]['name'];
						$v = $file['tmp_name'];
						if ($v != ""){
							if (isset($setupdata['file_size']) && ($file['size']/1024 > $setupdata['file_size'])) $err->AddError(28);
							else {
								$file_name = "";
								$today = uniqid("");
								$imagedata = getimagesize($v);
								if (!isset($imagedata['mime'])){
									if ($imagedata[2] == 1) $imagedata['mime'] = 'image/gif';
									elseif ($imagedata[2] == 2) $imagedata['mime'] = 'image/jpeg';
                  elseif ($imagedata[2] == 3) $imagedata['mime'] = 'image/png';
                  elseif ($imagedata[2] == 6) $imagedata['mime'] = 'image/bmp';
								}
								if ($imagedata === FALSE) $err->AddError(7);
								else {
									if ($imagedata['mime'] == 'image/jpeg') $file_name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".jpg";
									elseif ($imagedata['mime'] == 'image/pjpeg') $file_name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".jpeg";
									elseif ($imagedata['mime'] == 'image/gif') $file_name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".gif";
                  elseif ($imagedata['mime'] == 'image/png') $file_name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".png";
                  elseif ($imagedata['mime'] == 'image/bmp') $file_name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".bmp";
									else $err->AddError(7);
									if (strlen($file_name) > 3){
										$file_to = $file_name;
										if (strlen($file_name1) <= 0) $file_name1=$_FILES['img_file1']['name'];
										if (!(file_exists($setupdata['binary_path']."/".$parent))){
											if ((mkdir($setupdata['binary_path']."/".$parent, 0777)) && (copy($file['tmp_name'], $setupdata['binary_path']."/".$parent."/".$file_to))){
												if (file_exists($setupdata['binary_path']."/".$parent."/".$file_to)){
													chmod($setupdata['binary_path']."/".$parent."/".$file_to, 0666);
													$v = $imageclass->image_add($file, $file_name1, $file_to, $parent, $imagedata[0], $imagedata[1], $_SESSION['login_user']);
												}
											}
										} else {
											if (copy($file['tmp_name'], $setupdata['binary_path']."/".$parent."/".$file_to)){
												if (file_exists($setupdata['binary_path']."/".$parent."/".$file_to)){
													chmod($setupdata['binary_path']."/".$parent."/".$file_to, 0666);
													$v = $imageclass->image_add($file, $file_name1, $file_to, $parent, $imagedata[0], $imagedata[1], $_SESSION['login_user']);
												}
											}
										}
									}
								}
							}
						}
					}
				} else { 
					if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
				}
				if ((isset($_GET[$n])) or (isset($_POST[$n]))) {
					$v = $treeclass->post_text_parsing($v);
					$realstateclass->mod_product_fields($parent, $value, $v, $product_lang);
				} else {
					if ($t == 'price') $realstateclass->mod_product_fields($parent, $value, $v, $product_lang);
					elseif ($t == 'chekbox') $realstateclass->mod_product_fields($parent, $value, "", $product_lang);
				}
			}
		}
	} elseif ($mod=="files"){
		if ($act == 'del') {
			$product_lang = NULL;
			if (isset($_GET["product_lang"])) $product_lang=$_GET["product_lang"];
			elseif (isset($_POST["product_lang"])) $product_lang=$_POST["product_lang"];
			if ($product_lang != NULL) $realstateclass->file_del_with_parent_data($parent, $field_name, $image_id, $product_lang);
		}
	} elseif ($mod=="curency"){
		if ($act == 'mod') {
			$cur1 = array(); $cur2 = array(); 
			$cur1 = $realstateclass->get_curency();
			foreach($_POST as $key=>$el){
				if (array_key_exists($key, $cur1)) $cur2[$key] = $el;
			}
			$realstateclass->mod_curency($cur2);
		} elseif ($act == 'load') {
			$realstateclass->get_curency_from_lb();
		}
	} elseif ($mod=="off"){
		if ($act == 'hide') {
			if ($id > 0){
				$realstateclass->off_set_hide($id, $hide);
			}
		} elseif ($act == 'add_off') {
			$type_of_group = NULL;
			if (isset($_GET["type_of_group"])) $type_of_group=$_GET["type_of_group"];
			elseif (isset($_POST["type_of_group"])) $type_of_group=$_POST["type_of_group"];
			if (($name != NULL) && ($type_of_group != NULL)){
				$res = $realstateclass->off_main_group_add($name, $type_of_group);
				if ($res != 0) $err->AddError($res);
			} else $err->AddError(15);
		} elseif ($act == 'del_group') {
			$del_mas = array();
			if (isset($_GET["ids"])) $del_mas=$_GET["ids"];
			elseif (isset($_POST["ids"])) $del_mas=$_POST["ids"];
			if (count($del_mas) > 0){
				$realstateclass->off_group_del($del_mas);
			}
		} elseif ($act == 'rename'){
			if ($parent != NULL){
				if (strlen($name)  > 0) $realstateclass->off_rename($parent, $name);
				else $err->AddError(5);
			}
		} elseif ($act == 'edit_off'){
			if ($parent != NULL){
				$off = array();
				$off['desc'] = NULL; $off['num'] = NULL; $off['proc'] = NULL; 
				if (!isset($_GET['off_desc'])) $off['desc']=NULL; else $off['desc']=$_GET['off_desc'];
				if ($off['desc']==NULL) if (!isset($_POST['off_desc'])) $off['desc']=NULL; else $off['desc']=$_POST['off_desc'];
				if (!isset($_GET['off_num'])) $off['num']=NULL; else $off['num']=$_GET['off_num'];
				if ($off['num']==NULL) if (!isset($_POST['off_num'])) $off['num']=NULL; else $off['num']=$_POST['off_num'];
				$realstateclass->mod_off_data($parent, $off);
			}
		} elseif ($act == 'set_off_depend'){
			$ids = NULL; $mod_type = NULL;
			if (isset($_POST["off_type"])) $ids=$_POST["off_type"];
			if (($parent != NULL) && ($type != NULL)){
				$realstateclass->set_off($parent, $ids, $type);
			}
		}
	}
?>